package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Runtimecoursearrangeinfodetail;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinforecord;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.service.RuntimeCourseArrangeInfoDetailService;
import com.grad.why.studentsystem.service.RuntimeCourseArrangeInfoRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class RuntimeArrivedInfoController {
    @Autowired
    private RuntimeCourseArrangeInfoDetailService runtimeCourseArrangeInfoDetailService;

    @Autowired
    private RuntimeCourseArrangeInfoRecordService runtimeCourseArrangeInfoRecordService;

    @RequestMapping("/getRuntimeStudentArriveInfos")
    @ResponseBody
    public Object getRuntimeStudentArriveInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order,
            HttpServletRequest request
    ) {
        try {
            List<Runtimecoursearrangeinfodetail> rows = null;
            long total = 0;
            HttpSession session = request.getSession();
            if((int)session.getAttribute("type") == 2){
                int bindid = (int)session.getAttribute("bindid");
                rows = runtimeCourseArrangeInfoDetailService.getRuntimeStudentArriveInfoListByTeacherId(offset,limit,search,bindid);
                total = runtimeCourseArrangeInfoDetailService.getRuntimeStudentArriveInfoListLengthByTeacherId(search,bindid);
            }else{
                rows = runtimeCourseArrangeInfoDetailService.getRuntimeStudentArriveInfoList(offset, limit, sort, search);
                total = runtimeCourseArrangeInfoDetailService.getRuntimeStudentArriveInfoListLength(search);
            }
            if(rows!=null) {
                for (int i = 0; i < rows.size(); i++) {
                    Runtimecoursearrangeinfodetail runtimecoursearrangeinfodetail = rows.get(i);
                    runtimecoursearrangeinfodetail.setRealnamelabels(runtimecoursearrangeinfodetail.getRealnamelabels().replaceAll("\\|", ","));
                }
            }


            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/checkRuntimeArrivedInfosIsExist/{coursearrangeid}")
    @ResponseBody
    public String checkRuntimeArrivedInfosIsExist(@PathVariable("coursearrangeid") String coursearrangeid){
        try{
            Integer courseArrangeId = Integer.parseInt(coursearrangeid);
            int n = runtimeCourseArrangeInfoDetailService.checkCourseArrangeIdExist(courseArrangeId);
            if(n>0){
                return "OK";
            }else{
                return null;
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addRuntimeStudentArrivedInfo/{courseArrangeId}")
    @ResponseBody
    public List<String> addRuntimeStudentArrivedInfo(@PathVariable("courseArrangeId") String courseArrangeId,String infos) {
        try{
            Runtimecoursearrangeinfodetail runtimecoursearrangeinfodetail = runtimeCourseArrangeInfoDetailService.getRuntimeCourseArrangeInfoDetailByCourseArrangeId(Integer.parseInt(courseArrangeId));
            String realArrivedInfos = runtimecoursearrangeinfodetail.getRealnamelabels();
            if(realArrivedInfos.contains(infos)){
                return null;
            }else {
                if(runtimecoursearrangeinfodetail.getRealnamelabels().equals("")){
                    runtimecoursearrangeinfodetail.setRealnamelabels(infos);
                }else{
                    runtimecoursearrangeinfodetail.setRealnamelabels(runtimecoursearrangeinfodetail.getRealnamelabels() + "|" +infos);
                }
                runtimecoursearrangeinfodetail.setRealcount(runtimecoursearrangeinfodetail.getRealcount() + 1);


                List<String> notArrivedStudentNameLabels = new ArrayList<>();
                List<String> realArrviedStudentNameLabels = new ArrayList<>();
                if (runtimecoursearrangeinfodetail != null) {
                    String shouldArrivedStudentNameLabel = runtimecoursearrangeinfodetail.getShouldnamelabels();
                    String[] shouldArrivedStudentNameLabelstrs = shouldArrivedStudentNameLabel.split(",");

                    String realArrivedStudentNameLabel = runtimecoursearrangeinfodetail.getRealnamelabels();
                    String[] realArrivedStudentNameLabelstrs = realArrivedStudentNameLabel.split("\\|");

                    for (String rs : realArrivedStudentNameLabelstrs) {
                        realArrviedStudentNameLabels.add(rs);
                    }

                    for (int i = 0; i < shouldArrivedStudentNameLabelstrs.length; i++) {
                        if (!realArrviedStudentNameLabels.contains(shouldArrivedStudentNameLabelstrs[i])) {
                            notArrivedStudentNameLabels.add(shouldArrivedStudentNameLabelstrs[i]);
                        }
                    }
                    int n = runtimeCourseArrangeInfoDetailService.updateByEntity(runtimecoursearrangeinfodetail);
                    if(notArrivedStudentNameLabels.size() == 0){
                        notArrivedStudentNameLabels.add("OK");
                    }
                    return notArrivedStudentNameLabels;
                }
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getRuntimeNotArrivedStudentNameLabelsByCourseArrangeId/{coursearrangeid}")
    @ResponseBody
    public List<String> getRuntimeNotArrivedStudentNameLabelsByCourseArrangeId(@PathVariable("coursearrangeid")String coursearrangeid){
        try{
            Integer courseArrangeId = Integer.parseInt(coursearrangeid);
            List<String> notArrivedStudentNameLabels = new ArrayList<>();
            List<String> realArrviedStudentNameLabels = new ArrayList<>();

            Runtimecoursearrangeinfodetail runtimecoursearrangeinfodetail = runtimeCourseArrangeInfoDetailService.getRuntimeCourseArrangeInfoDetailByCourseArrangeId(courseArrangeId);

            if(runtimecoursearrangeinfodetail!=null){
                String shouldArrivedStudentNameLabel = runtimecoursearrangeinfodetail.getShouldnamelabels();
                String[] shouldArrivedStudentNameLabelstrs = shouldArrivedStudentNameLabel.split(",");

                String realArrivedStudentNameLabel = runtimecoursearrangeinfodetail.getRealnamelabels();
                String[] realArrivedStudentNameLabelstrs = realArrivedStudentNameLabel.split("\\|");

                for(String rs : realArrivedStudentNameLabelstrs){
                    realArrviedStudentNameLabels.add(rs);
                }

                for(int i = 0;i<shouldArrivedStudentNameLabelstrs.length;i++){
                    if(!realArrviedStudentNameLabels.contains(shouldArrivedStudentNameLabelstrs[i])){
                        notArrivedStudentNameLabels.add(shouldArrivedStudentNameLabelstrs[i]);
                    }
                }
                return notArrivedStudentNameLabels;
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
